/**
 * 
 */
package com.google.code.jkippt.http;

import java.util.Calendar;

import org.apache.http.client.utils.URIBuilder;

/**
 * @author Federico De Faveri defaveri@gmail.com
 *
 */
public class EndPointFactory {
	
	public static final String SERVER_BASE_URL = "https://kippt.com";
	public static final String ACCOUNT_END_POINT = SERVER_BASE_URL+"/api/account/";
	public static final String CLIP_END_POINT = SERVER_BASE_URL+"/api/clips/";
	public static final String LIST_END_POINT = SERVER_BASE_URL+"/api/lists/";
	public static final String SEARCH_END_POINT = SERVER_BASE_URL+"/api/search/clips/";
	
	protected static EndPointFactory instance;
	
	public static EndPointFactory getInstance()
	{
		if (instance==null) instance = new EndPointFactory();
		return instance;
	}
	
	/**
	 * Returns the Account endpoint.
	 * @return
	 */
	public String getAccountEndPoint()
	{
		return ACCOUNT_END_POINT;
	}
	
	/**
	 * Returns the Clips endpoint.
	 * @return
	 */
	public String getClipEndPoint()
	{
		return CLIP_END_POINT;
	}
	
	/**
	 * Returns the Clips endpoint for the specified Clip id.
	 * @param id
	 * @return
	 */
	public String getClipEndPoint(String id)
	{
		return CLIP_END_POINT+id+"/";
	}
	
	/**
	 * Returns the Clips endpoint for the specified parameters.
	 * @param list the list id or <code>null</code>
	 * @param since the since date or <code>null</code>
	 * @return
	 */
	public String getClipEndPoint(String list, Calendar since)
	{
		try {
			URIBuilder builder = new URIBuilder(CLIP_END_POINT);

			if (list!=null) builder.addParameter("list", list);

			if (since!=null) {
				String sinceValue = String.valueOf(since.getTimeInMillis());
				builder.addParameter("since", sinceValue);
			}
			
			return builder.build().toString();
		} catch (Exception e)
		{
			throw new IllegalArgumentException("Invalid parameters", e);
		}
	}
	
	/**
	 * Returns the Lists endpoint.
	 * @return
	 */
	public String getListEndPoint()
	{
		return LIST_END_POINT;
	}
	
	/**
	 * Returns the Lists endpoint for the specified id.
	 * @param id
	 * @return
	 */
	public String getListEndPoint(String id)
	{
		return LIST_END_POINT+id+"/";
	}
	
	/**
	 * Returns the next page endpoint.
	 * @param next
	 * @return
	 */
	public String getNextPageEndPoint(String next)
	{
		return SERVER_BASE_URL + next;
	}
	
	/**
	 * Returns the Search endpoint.
	 * @param query the query.
	 * @return
	 */
	public String getSearchEndPoint(String query)
	{
		return getSearchEndPoint(query, null, null);
	}
	
	/**
	 * Returns the Search endpoint.
	 * @param query the query.
	 * @param listId the list id or <code>null</code>
	 * @param isStarred stared flag or <code>null</code>
	 * @return
	 */
	public String getSearchEndPoint(String query, String listId, Boolean isStarred)
	{
		try {
			URIBuilder builder = new URIBuilder(SEARCH_END_POINT);
			builder.addParameter("q", query);
			if (listId!=null) builder.addParameter("list", listId);
			if (isStarred!=null) builder.addParameter("is_starred", String.valueOf(isStarred));
			return builder.build().toString();
		} catch (Exception e)
		{
			throw new IllegalArgumentException("Invalid query", e);
		}
	}

}
